//
//  FindSpotViewController.m
//  SXH
//
//  Created by 房 国生 on 15/7/22.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "FindSpotViewController.h"
#import "FindSpotTableViewCell.h"
#import "CommonService.h"
#import "UIButton+WebCache.h"
#import "FindDetailViewController.h"
#import "FindMeViewController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"

#import "HomeFind.h"
#import "FindEntity.h"

@interface FindSpotViewController ()<FindSpotDelegate>
{
    MBProgressHUD *HUD;
    MJRefreshHeaderView *_header;
    MJRefreshFooterView *_footer;
    int page;
}
@property (nonatomic, strong) IBOutlet UITableView *tableViewContent;
@property (nonatomic, strong) IBOutlet NSMutableArray *mutableArrayFind;

@end

@implementation FindSpotViewController
@synthesize tableViewContent, mutableArrayFind;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    mutableArrayFind = [[NSMutableArray alloc] init];
    
    page = 1;
    [self addHeader];
    [self addFooter];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [mutableArrayFind count];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *couponTableViewCellIdentifier = @"FindSpotTableViewCell";
    FindSpotTableViewCell *cell = (FindSpotTableViewCell *)[tableView dequeueReusableCellWithIdentifier:couponTableViewCellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FindSpotTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
        cell.findSpotDelegate = self;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    HomeFind *entity = [mutableArrayFind objectAtIndex:indexPath.row];
    
    cell.labelTitle.text = entity.parkname;
    cell.labelCount.text = [NSString stringWithFormat:@"%@个人发表", entity.travelCount];
    
    NSArray *arrayBtn = [NSArray arrayWithObjects:cell.btn1, cell.btn2, cell.btn3, cell.btn4, nil];
    
    for (int i = 0; i < 4 && entity.travelRows != nil && i < [entity.travelRows count]; i++) {
        FindEntity *findEntity = [entity.travelRows objectAtIndex:i];
        UIButton *btn = arrayBtn[i];
        [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:findEntity.album_thumb] forState:UIControlStateNormal];
        btn.hidden = NO;
        btn.tag = [findEntity.travel_id intValue];
    }
    cell.btnSpot.tag = [entity.parkid integerValue];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 109;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)findSpotButtonAction:(NSInteger) tag {
    FindEntity *entity = [[FindEntity alloc]init];
    entity.ID = [NSString stringWithFormat:@"%ld", (long)tag];
    FindDetailViewController *vc = [[FindDetailViewController alloc] init];
    vc.findEntity = entity;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void) getHomeFindData :(int) pageNo RefreshView:(MJRefreshBaseView *)refreshView{
    
    NSString *cid = [[NSUserDefaults standardUserDefaults] valueForKey:SelectedCityId];
    
    NSString *strPageNo = [NSString stringWithFormat:@"%d", pageNo];
    
    CommonService *service = [[CommonService alloc] init];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           cid,@"cityid",
                           strPageNo, @"page",
                           @"20", @"limit", nil];
    
    NSLog(@"param:%@",param);
    [service getNetWorkData:param
                       Path:QUERY_HOME_TRAVEL
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *)entity;
                      NSArray *array = [dic objectForKey:@"rows"];
                      for (NSDictionary* attributes in array) {
                          HomeFind *entity = [[HomeFind alloc] init];
                          entity = [entity initWithAttributes:attributes];
                          [mutableArrayFind addObject:entity];
                      }
                      [self doneWithView:refreshView];
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      
                  }];
}


- (void)addFooter
{
    //__unsafe_unretained OAAnnoViewController *vc = self;
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableViewContent;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        
        ++page;
        [self getHomeFindData:page RefreshView:refreshView];
        
        NSLog(@"%@----开始进入刷新状态", refreshView.class);
    };
    _footer = footer;
}

- (void)addHeader
{
    //__unsafe_unretained OAAnnoViewController *vc = self;
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableViewContent;
    
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        // 进入刷新状态就会回调这个Block
        [mutableArrayFind removeAllObjects];
        [self getHomeFindData:1 RefreshView:refreshView];
        NSLog(@"%@----开始进入刷新状态", refreshView.class);
    };
    header.endStateChangeBlock = ^(MJRefreshBaseView *refreshView) {
        // 刷新完毕就会回调这个Block
        NSLog(@"%@----刷新完毕", refreshView.class);
    };
    header.refreshStateChangeBlock = ^(MJRefreshBaseView *refreshView, MJRefreshState state) {
        // 控件的刷新状态切换了就会调用这个block
        switch (state) {
            case MJRefreshStateNormal:
                NSLog(@"%@----切换到：普通状态", refreshView.class);
                break;
            case MJRefreshStatePulling:
                NSLog(@"%@----切换到：松开即可刷新的状态", refreshView.class);
                break;
            case MJRefreshStateRefreshing:
                NSLog(@"%@----切换到：正在刷新状态", refreshView.class);
                break;
            default:
                break;
        }
    };
    [header beginRefreshing];
    _header = header;
}

- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    // 刷新表格
    [self.tableViewContent reloadData];
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}

- (void)findMeButtonAction:(NSInteger) tag {
    FindMeViewController *vc = [[FindMeViewController alloc] init];
    vc.userId = [NSString stringWithFormat:@"%d", (int)tag];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"FindSpotViewController"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"FindSpotViewController"];
}
@end
